home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxdcconv.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.0 KB  |  162 lines

  1. /* Copyright (C) 1992, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxdcconv.c,v 1.3 2000/09/19 19:00:35 lpd Exp $ */
  20. /* Conversion between device color spaces for Ghostscript */
  21. #include "gx.h"
  22. #include "gsdcolor.h"        /* for gxcmap.h */
  23. #include "gxdcconv.h"        /* interface */
  24. #include "gxdevice.h"        /* for gxcmap.h */
  25. #include "gxcmap.h"
  26. #include "gxfarith.h"
  27. #include "gxlum.h"
  28. #include "gxistate.h"
  29.  
  30. /*
  31.  * The CMYK to RGB algorithms specified by Adobe are, e.g.,
  32.  *      R = 1.0 - min(1.0, C + K)
  33.  *      C = max(0.0, min(1.0, 1 - R - UCR))
  34.  * We got better results on displays with
  35.  *      R = (1.0 - C) * (1.0 - K)
  36.  *      C = max(0.0, min(1.0, 1 - R / (1 - UCR)))
  37.  * For utmost compatibility, we offer the Adobe algorithms as an option:
  38.  */
  39. #define USE_ADOBE_CMYK_RGB
  40.  
  41. /* ------ Color space conversion ------ */
  42.  
  43. /* Only 4 of the 6 conversions are implemented here; */
  44. /* the other 2 (Gray to RGB/CMYK) are trivial. */
  45.  
  46. /* Convert RGB to Gray. */
  47. frac
  48. color_rgb_to_gray(frac r, frac g, frac b, const gs_imager_state * pis)
  49. {
  50.     return (r * (unsigned long)lum_red_weight +
  51.         g * (unsigned long)lum_green_weight +
  52.         b * (unsigned long)lum_blue_weight +
  53.         (lum_all_weights / 2))
  54.     / lum_all_weights;
  55. }
  56.  
  57. /* Convert RGB to CMYK. */
  58. /* Note that this involves black generation and undercolor removal. */
  59. void
  60. color_rgb_to_cmyk(frac r, frac g, frac b, const gs_imager_state * pis,
  61.           frac cmyk[4])
  62. {
  63.     frac c = frac_1 - r, m = frac_1 - g, y = frac_1 - b;
  64.     frac k = (c < m ? min(c, y) : min(m, y));
  65.  
  66.     /*
  67.      * The default UCR and BG functions are pretty arbitrary,
  68.      * but they must agree with the ones in gs_init.ps.
  69.      */
  70.     frac bg =
  71.     (pis->black_generation == NULL ? frac_0 :
  72.      gx_map_color_frac(pis, k, black_generation));
  73.     signed_frac ucr =
  74.     (pis->undercolor_removal == NULL ? frac_0 :
  75.      gx_map_color_frac(pis, k, undercolor_removal));
  76.  
  77.     if (ucr == frac_1)
  78.     cmyk[0] = cmyk[1] = cmyk[2] = 0;
  79.     else if (ucr == frac_0)
  80.     cmyk[0] = c, cmyk[1] = m, cmyk[2] = y;
  81.     else {
  82. #ifdef USE_ADOBE_CMYK_RGB
  83.     /* C = max(0.0, min(1.0, 1 - R - UCR)), etc. */
  84.     signed_frac not_ucr = (ucr < 0 ? frac_1 + ucr : frac_1);
  85.  
  86.     cmyk[0] = (c < ucr ? frac_0 : c > not_ucr ? frac_1 : c - ucr);
  87.     cmyk[1] = (m < ucr ? frac_0 : m > not_ucr ? frac_1 : m - ucr);
  88.     cmyk[2] = (y < ucr ? frac_0 : y > not_ucr ? frac_1 : y - ucr);
  89. #else
  90.     /* C = max(0.0, min(1.0, 1 - R / (1 - UCR))), etc. */
  91.     float denom = frac2float(frac_1 - ucr);        /* unscaled */
  92.     float v;
  93.  
  94.     v = (float)frac_1 - r / denom;    /* unscaled */
  95.     cmyk[0] =
  96.         (is_fneg(v) ? frac_0 : v >= (float)frac_1 ? frac_1 : (frac) v);
  97.     v = (float)frac_1 - g / denom;    /* unscaled */
  98.     cmyk[1] =
  99.         (is_fneg(v) ? frac_0 : v >= (float)frac_1 ? frac_1 : (frac) v);
  100.     v = (float)frac_1 - b / denom;    /* unscaled */
  101.     cmyk[2] =
  102.         (is_fneg(v) ? frac_0 : v >= (float)frac_1 ? frac_1 : (frac) v);
  103. #endif
  104.     }
  105.     cmyk[3] = bg;
  106.     if_debug7('c', "[c]RGB 0x%x,0x%x,0x%x -> CMYK 0x%x,0x%x,0x%x,0x%x\n",
  107.           r, g, b, cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
  108. }
  109.  
  110. /* Convert CMYK to Gray. */
  111. frac
  112. color_cmyk_to_gray(frac c, frac m, frac y, frac k, const gs_imager_state * pis)
  113. {
  114.     frac not_gray = color_rgb_to_gray(c, m, y, pis);
  115.  
  116.     return (not_gray > frac_1 - k ?    /* gray + k > 1.0 */
  117.         frac_0 : frac_1 - (not_gray + k));
  118. }
  119.  
  120. /* Convert CMYK to RGB. */
  121. void
  122. color_cmyk_to_rgb(frac c, frac m, frac y, frac k, const gs_imager_state * pis,
  123.           frac rgb[3])
  124. {
  125.     switch (k) {
  126.     case frac_0:
  127.         rgb[0] = frac_1 - c;
  128.         rgb[1] = frac_1 - m;
  129.         rgb[2] = frac_1 - y;
  130.         break;
  131.     case frac_1:
  132.         rgb[0] = rgb[1] = rgb[2] = frac_0;
  133.         break;
  134.     default:
  135.         {
  136. #ifdef USE_ADOBE_CMYK_RGB
  137.         /* R = 1.0 - min(1.0, C + K), etc. */
  138.         frac not_k = frac_1 - k;
  139.  
  140.         rgb[0] = (c > not_k ? frac_0 : not_k - c);
  141.         rgb[1] = (m > not_k ? frac_0 : not_k - m);
  142.         rgb[2] = (y > not_k ? frac_0 : not_k - y);
  143. #else
  144.         /* R = (1.0 - C) * (1.0 - K), etc. */
  145.         ulong not_k = frac_1 - k;
  146.  
  147.         /* Compute not_k * (frac_1 - v) / frac_1 efficiently. */
  148.         ulong prod;
  149.  
  150. #define deduct_black(v)\
  151.   (prod = (frac_1 - (v)) * not_k, frac_1_quo(prod))
  152.         rgb[0] = deduct_black(c);
  153.         rgb[1] = deduct_black(m);
  154.         rgb[2] = deduct_black(y);
  155. #undef deduct_black
  156. #endif
  157.         }
  158.     }
  159.     if_debug7('c', "[c]CMYK 0x%x,0x%x,0x%x,0x%x -> RGB 0x%x,0x%x,0x%x\n",
  160.           c, m, y, k, rgb[0], rgb[1], rgb[2]);
  161. }
  162.